#include "animatewindow.h"
#include <QPainter>
#include <QDebug>
#include <QTimer>

int timeinternal = 5;
AnimateWindow::AnimateWindow()
{
    setAttribute( Qt::WA_TranslucentBackground, true );
    resize(60, 60);

    QRegion window( 0,
                    0,
                    width(),
                    height(),
                    QRegion::Rectangle );

    QRegion mouseHole( width() / 2 - 1,
                       height() / 2 - 1,
                       2,
                       2,
                       QRegion::Rectangle );

    QRegion r1 = window.QRegion::subtracted( mouseHole );
    this->setMask( r1 );

    m_timer = new QTimer(this);
    m_timer->setTimerType( Qt::PreciseTimer );
    connect(m_timer, SIGNAL(timeout()), this, SLOT(calcSpeed()));
}

AnimateWindow::~AnimateWindow()
{
}

void AnimateWindow::setIsShowPath(bool value)
{
    m_isShowPath = value;
    if (m_isShowPath)
    {
        m_timer->start(timeinternal);
    }
    else
    {
        m_timer->stop();
    }
}

void AnimateWindow::setIsShowClick(bool value)
{
    m_isShowClick = value;
}

void AnimateWindow::setIsPressed(bool value)
{
    m_isPressed = value;
}

void AnimateWindow::paintEvent( QPaintEvent *)
{
    QPainter painter( this );
    painter.setRenderHints( QPainter::Antialiasing, true );

    if (m_isShowClick && m_isPressed)
    {
        QBrush brush;
        if ( radiant == false )
        {
            brush.setStyle( Qt::SolidPattern );
            brush.setColor( color );
        }
        else
        {
            QRadialGradient radialGradient( width()/2, height()/2, diameter/2 );
            radialGradient.setColorAt( 0, color );
            radialGradient.setColorAt( 1, Qt::transparent );
            QBrush brushRadial( radialGradient );
            brush.swap( brushRadial );
        }

        painter.setPen( Qt::NoPen );
        painter.setBrush( brush );
        painter.setOpacity ( opacity );
        painter.drawEllipse ( QPoint( width()/2, height()/2 ), diameter/2, diameter/2 );
    }

    if (m_isShowPath)
    {
        QPen pen;
        QLinearGradient linearGradient(0, 0, 100, 100);
        linearGradient.setColorAt(0.0, Qt::red);
        linearGradient.setColorAt(1.0, Qt::white);
        // 设置渐变作为pen的画刷
        pen.setBrush(linearGradient);
        pen.setWidth(4);
        painter.setPen(pen);
        painter.setOpacity ( opacity );
        for (int i = 0; i < trailPoints.size() - 1; ++i)
        {
            painter.drawLine(mapFromGlobal(trailPoints[i]), mapFromGlobal(trailPoints[i + 1]));
        }
    }
}

void AnimateWindow::setRadiusColor( int valueDiameter, QColor valueColor )
{
    diameter = valueDiameter;
    color = valueColor;
    repaint();
}

void AnimateWindow::setOpacity( double value )
{
    opacity = value;
}

void AnimateWindow::setRadiant( bool value )
{
    radiant = value;
}

void AnimateWindow::calcSpeed()
{
    if (trailPoints.count() == 0)
    {
        return;
    }

    if ((m_x == -1) && (m_y == -1))
    {
        m_x = trailPoints[trailPoints.count()-1].x();
        m_y = trailPoints[trailPoints.count()-1].y();
        return;
    }

    double speedX = 0;
    double speedY = 0;
    // 计算速率
    int dx = trailPoints[trailPoints.count()-1].x() - m_x;
    int dy = trailPoints[trailPoints.count()-1].y() - m_y;
    speedX = dx * 1000.0 / timeinternal; // 单位：像素/毫秒
    speedY = dy * 1000.0 / timeinternal;
//    qDebug() << "speedX:" << speedX;
//    qDebug() << "m_x:" << m_x;
//    qDebug() << "x:" << trailPoints[trailPoints.count()-1].x();
//    qDebug() << "speedY:" << speedY;
//    qDebug() << "m_y:" << m_y;
//    qDebug() << "y:" << trailPoints[trailPoints.count()-1].y();

    // 更新位置
    m_x = trailPoints[trailPoints.count()-1].x();
    m_y = trailPoints[trailPoints.count()-1].y();

    if ((speedX < 4) && (speedY < 4))
    {
        trailPoints.pop_front();
    }

    update();
}

void AnimateWindow::addTrailPoint(const QPoint &point)
{
    trailPoints.append(point);
    if (trailPoints.size() > maxTrailLength) {
        trailPoints.pop_front();
    }

    update();
}

